#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
二维码文件编码解码器
支持将文件编码为二维码序列和从二维码序列恢复文件
"""

import tkinter as tk
from tkinter import ttk, messagebox
import os

from sender_tab import SenderTab
from receiver_tab import ReceiverTab
from logger import Logger

class QRFileCoder:
    def __init__(self, root):
        """初始化主界面"""
        self.root = root
        self.root.title("QRFileCoder    (from firexmoon)")
        self.root.geometry("800x700")
        self.root.minsize(800, 700)
        
        # 创建必要的目录
        os.makedirs("qr", exist_ok=True)
        os.makedirs("out", exist_ok=True)
        
        self.create_widgets()
        
    def create_widgets(self):
        """创建主界面组件"""
        # 创建标签页控件
        self.notebook = ttk.Notebook(self.root)
        self.notebook.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
        
        # 创建编码器标签页
        self.sender_frame = ttk.Frame(self.notebook)
        self.notebook.add(self.sender_frame, text="编码器")
        self.sender_tab = SenderTab(self.sender_frame)
        
        # 创建解码器标签页
        self.receiver_frame = ttk.Frame(self.notebook)
        self.notebook.add(self.receiver_frame, text="解码器")
        self.receiver_tab = ReceiverTab(self.receiver_frame)
        
        # 创建日志区域
        log_frame = ttk.LabelFrame(self.root, text="操作日志", padding="10")
        log_frame.pack(fill=tk.BOTH, expand=False, padx=10, pady=(0, 10))
        
        self.logger = Logger(log_frame, height=24)
        self.sender_tab.set_logger(self.logger)
        self.receiver_tab.set_logger(self.logger)
        
        # 创建进度条
        progress_frame = ttk.Frame(self.root)
        progress_frame.pack(fill=tk.X, padx=10, pady=(0, 10))
        
        self.progress_var = tk.DoubleVar()
        self.progress_bar = ttk.Progressbar(
            progress_frame, 
            variable=self.progress_var, 
            maximum=100
        )
        self.progress_bar.pack(fill=tk.X, side=tk.TOP)
        
        self.progress_label = ttk.Label(progress_frame, text="就绪")
        self.progress_label.pack(side=tk.TOP, pady=(5, 0))
        # 设置标签页的进度引用
        self.sender_tab.set_progress(self.progress_var, self.progress_label)
        self.receiver_tab.set_progress(self.progress_var, self.progress_label)

def main():
    """主函数"""
    # 创建主窗口
    root = tk.Tk()
    
    # 创建应用实例
    app = QRFileCoder(root)
    
    # 运行主循环
    root.mainloop()

if __name__ == "__main__":
    main()